//
//  myEnterViewController.m
//  INAlienLand
//
//  Created by dllo on 15/7/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "myEnterViewController.h"
#import "ThirdViewController.h"

@interface myEnterViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *AddArr;
@property(nonatomic,retain)UITableView *MytableView;

@end

@implementation myEnterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor grayColor];
    //开启编辑
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    

    self.navigationItem.rightBarButtonItem.title = @"编辑";

    self.AddArr = [NSMutableArray array];
    
    
    [self unarech];
    [self mytableview];
    
}

- (void)unarech{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Url.plist"];
    self.AddArr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    
    
    
    
}


- (void)mytableview{
    
    self.MytableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.MytableView];
    self.MytableView.dataSource = self;
    self.MytableView.delegate = self;
    self.MytableView.rowHeight = self.view.frame.size.height/10;
    
    
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    [self.MytableView setEditing:editing animated:YES];
  
    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return UITableViewCellEditingStyleDelete;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //一定要注意:要操作的是数据源的那个数组
        
        [self.AddArr removeObjectAtIndex:indexPath.row];
    //删除后进行归档操作
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Url.plist"];
        [NSKeyedArchiver archiveRootObject:self.AddArr toFile:documentPath];
        
        //删除tableView中对应的cell,但是再删除之前一定要把数组中的数据先删除
        //第一个参数:数组,存放要删除那行的位置
        //第二个参数,设置删除行的动画效果
        [self.MytableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
        
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return @"删除收藏";
    
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
//    
//    [super setEditing:editing animated:animated];
//    [self.MytableView setEditing:editing animated:YES];
//}
//
//
//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//    return YES;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.AddArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = self.AddArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ThirdViewController *thired = [[ThirdViewController alloc]init];
    [self.navigationController pushViewController:thired animated:YES];
    thired.Url = self.AddArr[indexPath.row];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
